<template>
  <el-dialog :visible.sync="visible"
             :title="$t('updatePassword.title')"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             :append-to-body="true">
    <el-form :model="dataForm"
             :rules="dataRule"
             :inline="false"
             label-position="top"
             ref="dataForm"
             label-width="80px"
             v-loading="loading">
      <el-form-item :label="$t('updatePassword.username')">
        <span>{{ dataForm.username }}</span>
      </el-form-item>
       <el-form-item prop="newPassword"
                          :label="$t('label.password')">
              <el-input v-model="dataForm.newPassword"
                        type="password"
                        autocomplete="new-password"
                        show-password
                        :placeholder="$t('login.password')">
              </el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword"
                          :label="$t('label.cp')">
              <el-input v-model="dataForm.confirmPassword"
                        type="password"
                        autocomplete="new-password"
                        show-password
                        :placeholder="$t('login.repeatpassword')">
              </el-input>
            </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button size="small" @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button size="small" type="primary"
                 @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        userId: '',
        username: '',
        newPassword: '',
        confirmPassword: ''
      },
      loading: false
    }
  },
  computed: {
    dataRule () {
      let validateConfirmPassword = (rule, value, callback) => {
        try {
          if (value !== this.dataForm.newPassword) {
            callback(new Error(this.$t('custom.bots.design.passwordsNotMatch')))
          } else {
            callback()
          }
        } catch (error) {
          callback(new Error(this.$t('custom.bots.design.passwordsNotMatch')))
        }
      }
      return {
        newPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        if (this.$refs['dataForm']) this.$refs['dataForm'].resetFields()
      })
    },
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$interface.system.resetClinentPortalPassword(this.dataForm)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              this.loading = false
              return this.$message.error(res.msg)
            }
            this.loading = false
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.cancelDialog()
              }
            })
          }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    cancelDialog () {
      this.dataForm = {
        userId: '',
        username: '',
        newPassword: '',
        confirmPassword: ''
      }

      this.visible = false
    }
  }
}
</script>
